//
//  SISSMyKPIViewController.m
//  SISSAPP
//
//  Created by 袁应荣 on 2018/10/16.
//  Copyright © 2018年 优房网. All rights reserved.
//

#import "SISSMyKPIViewController.h"
#import "SISSMyKPICell.h"
#import "SISSKPIDetailViewController.h"
#import "SISSNetworkingManager+Mine.h"

@interface SISSMyKPIViewController ()<UITableViewDelegate,UITableViewDataSource>

/// tableView
@property (weak, nonatomic) UITableView *tableView;
/// 数据源
@property (strong, nonatomic) NSArray<SISSKPIListModel *> *KPIListArr;
// 保存旋转状态(展开/折叠);
@property (strong, nonatomic)  NSMutableArray *switchArr;

@end

@implementation SISSMyKPIViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    WeakSelf
    [kSISSRefresh prepareHeaderRefresh:self.tableView loadDataBlock:^{
        kSISSRefresh.refreshType = SISSRefressTypeUpDown;
        [weakSelf upDownLoadData];
    }];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self requestAchievementData];
}

#pragma mark - 上下拉刷新加载方法
- (void)upDownLoadData {
    [self requestAchievementData];
}

#pragma mark - UITableViewDelegate & UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.KPIListArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if ([self.switchArr[section] boolValue] == YES) {
        return self.KPIListArr[section].achievementList.count;
    } else {
        return 0;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SISSMyKPICell *cell = [tableView dequeueReusableCellWithIdentifier:myKPICellReuseID forIndexPath:indexPath];
    
    cell.achievementListModel = self.KPIListArr[indexPath.section].achievementList[indexPath.row];
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    SISSKPIDetailViewController *vc = [[SISSKPIDetailViewController alloc] init];
    vc.title = @"业绩指标申报详情";
    if(!(self.KPIListArr[indexPath.section].achievementList[indexPath.row].ID.length > 0)) return;
    vc.ID = self.KPIListArr[indexPath.section].achievementList[indexPath.row].ID;
    vc.isAppyIn = [self.KPIListArr[indexPath.section].achievementList[indexPath.row].business_state isEqualToString:@"1"];
    
    __weak typeof(self) weakSelf = self;
    vc.deleteComplete = ^{
        [weakSelf requestAchievementData];
    };
    [self.navigationController pushViewController:vc animated:YES];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView = [[UIView alloc] init];
    headerView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    UILabel *leftLabel = [[UILabel alloc] init];
    leftLabel.textColor = COLOR(0x334A60);
    leftLabel.font = [UIFont systemFontOfSize:14];
    [headerView addSubview:leftLabel];
    
    UILabel *rightLabel = [[UILabel alloc] init];
    rightLabel.textColor = COLOR(0x334A60);
    rightLabel.font = [UIFont systemFontOfSize:14];
    [headerView addSubview:rightLabel];
    
    CGFloat margin = 8.0;
    [leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(headerView).offset(1.5 * margin);
        make.left.equalTo(headerView).offset(2 * margin);
    }];
    [rightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(leftLabel);
        make.top.equalTo(leftLabel.mas_bottom).offset(margin);
    }];
    
    leftLabel.text = [NSString stringWithFormat:@"星级周期：%@-%@",self.KPIListArr[section].start_time,self.KPIListArr[section].end_time];
    rightLabel.text = [NSString stringWithFormat:@"周期总业绩值：%@",[PublicUtil returnTwoDeciTwoDecimalFormatter:self.KPIListArr[section].total_value]];
    headerView.userInteractionEnabled = YES;
    // 初始化一个手势
    UITapGestureRecognizer *myTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(openClick:)];
    // 给view添加手势
    [headerView addGestureRecognizer:myTap];
    headerView.tag = 1000 + section;
    return headerView;
}

/// 展开关闭点击事件
- (void)openClick:(UITapGestureRecognizer *)sender {
    
    NSInteger section = sender.view.tag - 1000;
    if ([self.switchArr[section] boolValue] == NO) {
        [self.switchArr replaceObjectAtIndex:section withObject:@YES];
    } else {
        [self.switchArr replaceObjectAtIndex:section withObject:@NO];
    }
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationAutomatic];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 66;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 11;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}
#pragma mark - 加载数据
- (void)requestAchievementData {
    WeakSelf
    [kNetworkManager requestAchievementListWithSuccessBlock:^(NSArray<SISSKPIListModel *> *KPIListArr) {
        
        self.KPIListArr = KPIListArr;
//        [self.switchArr removeAllObjects];
        if (!(weakSelf.switchArr.count > 0)) {
            for (int i = 0; i < KPIListArr.count; i++) {
                if (KPIListArr[i].achievementList.count > 0) {
                    [self.switchArr addObject:@YES];
                } else {
                    [self.switchArr addObject:@NO];
                }
            }
        }
        [self.tableView.mj_header endRefreshing];
        [self.tableView reloadData];
        
    } failed:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}

#pragma mark - UI
- (void)setupSubviews {
    [super setupSubviews];
    
    UITableView *tableView = [[UITableView alloc] init];
    tableView.dataSource = self;
    tableView.delegate = self;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [tableView registerClass:[SISSMyKPICell class] forCellReuseIdentifier:myKPICellReuseID];
    [self.view addSubview:tableView];
    self.tableView = tableView;
}

#pragma mark - 约束
- (void)initConstraint {
    [super initConstraint];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
}

- (NSArray<SISSKPIListModel *> *)KPIListArr {
    if (!_KPIListArr) {
        _KPIListArr = [NSArray array];
    }
    return _KPIListArr;
}

- (NSMutableArray *)switchArr {
    if (!_switchArr) {
        _switchArr = [NSMutableArray array];
    }
    return _switchArr;
}

@end
